package com.bnz.manager.service.impl;

import com.bnz.common.PageUtils;
import com.bnz.manager.entity.SpecificationOptionEntity;
import com.bnz.manager.entity.group.Specification;
import com.bnz.manager.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.Query;

import com.bnz.manager.dao.SpecificationDao;
import com.bnz.manager.entity.SpecificationEntity;
import com.bnz.manager.service.SpecificationService;
import org.springframework.transaction.annotation.Transactional;


@Service("specificationService")
@Transactional          // 加事务
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {
    // 引入规格选项
    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    // 添加规格和规格选项
    @Override
    public void insert(Specification specification) {
        // 1. 先构造一个规格,SpecificationEntity是规格表
        SpecificationEntity specificationEntity = new SpecificationEntity();
        // 2. 新添加一个规格，将Specification里面得到的表添加到规格表中
        specificationEntity.setSpecName(specification.getSpecName());
        // 3. 保存
        this.save(specificationEntity);
        // 4. 遍历，将规格选项和规格关联的ID匹配上
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            // 4.1 将规格选项和规格关联的ID匹配上
            option.setSpecId(specificationEntity.getId());
            // 4.2 保存
            optionService.save(option);
        }
    }

    // 根据规格id查询规格的相关信息
    @Override
    public Specification findById(String id) {
        // 1. 根据id查询出对应的规格
        SpecificationEntity spec = this.getById(id);
        // 2. 根据规格id查询出对应的规格选项列表
        List<SpecificationOptionEntity> options =
                optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
        // 返回规格和规格选项的信息
        return new Specification(id,spec.getSpecName(),options);
    }

    // 修改规格和规格选项
    @Override
    public void updateSpec(Specification specification) {
        // 1. 定义我们要修改的规格对象
        SpecificationEntity entity = new SpecificationEntity(specification.getId(),specification.getSpecName());
        this.updateById(entity);
        // 2. 修改规格和规格选项，思想是先删除，再重新添加
        // 3. 根据id删除原来的规格选项
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",specification.getId()));
        // 4. 重新添加规格选项
        List<SpecificationOptionEntity> options = specification.getOptions();
        // 5. 遍历
        for (SpecificationOptionEntity option : options) {
            // 5.1 重新关联规格id和规格选项绑定的id
            option.setSpecId(specification.getId());
            // 5.2 添加规格选项
            optionService.save(option);
        }
    }

    // 根据id删除对应的规格，如果存在主外键的关系则要先删除非主表
    @Override
    public void delete(List<String> ids) {
        // 1. 删除规格
        this.removeByIds(ids);
        // 2. 删除规格选项（方法一）
//        for (String id : ids) {
//            optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
//        }
        // 3. 删除规格选项（方法二）
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().in("spec_id",ids));
    }

}